﻿// -----------------------------------------------------------------------
// <copyright file="ViewModelRailroads.cs" company="">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace RauteAdministarion.ViewModels
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Collections.ObjectModel;
    using RailroadDal;
    using RauteAdministarion.Models;
    using System.ComponentModel;
    using System.Windows.Data;
    using System.Windows.Input;
    using RauteAdministarion.Commands;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class ViewModelRailroads:ViewModelBase
    {
        private ModelRautes model;
        private ICommand addStation;
        private ICommand updateStation;
        private ICommand deleteStation;
        private ICommand deleteDistance;

        public ICommand DeleteDistance
        {
            get
            {
                if (deleteDistance == null)
                {
                    this.deleteDistance = new ParameterRelayCommand((object obj) =>
                    {

                        this.model.DeleteDistance((int)obj);
                        this.model.UpdateDistances();
                    });
                }
                return deleteDistance;
            }
        }

        public ICommand DeleteStation
        {
            get
            {
                if (deleteStation == null)
                {
                    this.deleteStation = new ParameterRelayCommand((object obj) =>
                    {

                        this.model.DeleteStation((int)obj);
                            this.operationResult = "Adding Successful!";
                        
                            this.operationResult = "Adding Unsuccessful!";
                        

                        model.ReloadStations();
                        this.OnPropertyChanged("Stations");

                        var view = GetDefaultCategoriesView();
                        view.MoveCurrentToFirst();
                        this.OnPropertyChanged("OperationResult");
                    });
                }
                return deleteStation;
            }
        }

        public ICommand UpdateStation
        {
            get
            {
                if (updateStation == null)
                {
                    this.updateStation = new ParameterRelayCommand((object obj) =>
                    {
                        try
                        {
                            this.model.UpdateStation(obj as Station);
                            this.operationResult = "Adding Successful!";
                        }
                        catch
                        {
                            this.operationResult = "Adding Unsuccessful!";
                        }

                        model.ReloadStations();
                        this.OnPropertyChanged("Stations");

                        var view = GetDefaultCategoriesView();
                        view.MoveCurrentToFirst();
                        this.OnPropertyChanged("Stations");
                    });
                }
                return updateStation;
            }
        }

        public ICommand AddStation
        {
            get
            {
                if (addStation == null)
                {
                    this.addStation = new ParameterRelayCommand((object obj) =>
                    {
                        try
                        {
                            this.model.CreateStation(obj as Station);
                            this.operationResult = "Adding Successful!";
                        }
                        catch
                        {
                            this.operationResult = "Adding Unsuccessful!";
                        }

                        model.ReloadStations();
                        this.OnPropertyChanged("Stations");

                        var view = GetDefaultCategoriesView();
                        view.MoveCurrentToPosition(1);
                        this.OnPropertyChanged("Stations");
                    });
                }
                return addStation;
            }
        }

        public ObservableCollection<Station> Stations
        {
            get
            {
               
                return this.model.Stations;
            }
        }

        public ObservableCollection<Distance> Distances
        {
            get
            {
                return this.model.Distances;
            }
        }

        public ViewModelRailroads()
        {
            this.model = new ModelRautes();
            var view = GetDefaultCategoriesView();
            view.CurrentChanged += new EventHandler(view_CurrentChanged);
        }

        public Station SelectedStation
        {
            get
            {
                return GetDefaultCategoriesView().CurrentItem as Station;
            }
        }

        private ICollectionView GetDefaultCategoriesView()
        {
            var view = CollectionViewSource.GetDefaultView(this.Stations);
            return view;
        }

        void view_CurrentChanged(object sender, EventArgs e)
        {
            OnPropertyChanged("SelectedStation");
            OnPropertyChanged("Products");
        }
    }
}
